﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BastInfos.Models.Event;
using DataAccess;
using DataAccess.Helpers;
using BastInfos.Helpers;
using BastInfos.Controls;
using System.Drawing;

namespace BastInfos.Controllers
{
	public class EventController : ControllerBase
	{
		[HandleError(ExceptionType = typeof(InvalidOperationException), View = "404")]
		public ActionResult Detail(string title, int id)
		{
			return View(new EventModel(DataRepository.Get<Event>()
				.First(e => e.id_Event == id && (e.Status == (byte)StatusType.Visible/* || UserMembership.IsAuthenticated*/))));
		}

		[HttpPost]
		[HandleError(ExceptionType = typeof(InvalidOperationException), View = "404")]
		public ActionResult Detail(string title, int id, EventModel model, Comment comment)
		{
			if (ModelState.IsValid)
			{
				comment.CreationDate = DateTime.Now;
				comment.id_Item = id;
				comment.Validation = (byte)ValidationType.Attente;
				comment.CommentType = (byte)CommentType.Evenement;

				DataRepository.Context.Comments.InsertOnSubmit(comment);
				DataRepository.Context.SubmitChanges();

				ViewBag.ShowConfirm = true;

				JavaScriptHelper.Instance.WriteScript("$('#spnMessage').fadeIn(500).delay(5000).fadeOut(2500);");
			}
			return View(new EventModel(DataRepository.Get<Event>()
				.First(e => e.id_Event == id && (e.Status == (byte)StatusType.Visible/* || UserMembership.IsAuthenticated*/))));
		}

		public ActionResult List()
		{
			int total = DataRepository.Get<Event>().Count(e => e.Status == (byte)StatusType.Visible);
			int page = GetPage(total);

			IEnumerable<Event> events = DataRepository.Get<Event>()
				.Where(e => e.Status == (byte)StatusType.Visible)
				.OrderByDescending(e => e.CreationDate)
				.Skip((page - 1) * PAGE_SIZE)
				.Take(PAGE_SIZE);

			Calendar calendar1 = null;
			Calendar calendar2 = null;
			SetCalendar(events, out calendar1, out calendar2);

			return View(new EventListModel { CurrentPage = page, PageSize = PAGE_SIZE, TotalItems = total, Events = events, Calendar1 = calendar1, Calendar2 = calendar2 });
		}

		[HandleError(ExceptionType = typeof(InvalidOperationException), View = "404")]
		public ActionResult Category(string description, int id)
		{
			Category category = DataRepository.Get<Category>().First(c => c.Description == description && c.id_Category == id);
			int total = DataRepository.Get<Event>().Count(e => e.Status == (byte)StatusType.Visible && e.id_Category == category.id_Category);
			int page = GetPage(total);

			IEnumerable<Event> events = DataRepository.Get<Event>()
				.Where(e => e.Status == (byte)StatusType.Visible && e.id_Category == category.id_Category)
				.OrderByDescending(e => e.CreationDate)
				.Skip((page - 1) * PAGE_SIZE)
				.Take(PAGE_SIZE);

			Calendar calendar1 = null;
			Calendar calendar2 = null;
			SetCalendar(events, out calendar1, out calendar2);

			return View("List", new EventListModel { CurrentPage = page, PageSize = PAGE_SIZE, TotalItems = total, Events = events, Calendar1 = calendar1, Calendar2 = calendar2, Category = category });
		}

		private static void SetCalendar(IEnumerable<Event> events, out Calendar calendar1, out Calendar calendar2)
		{
			calendar1 = new Calendar(DateTime.Now.Month, DateTime.Now.Year).Css("calendar_big").ShowMonth(true).ShowYear(true);
			calendar2 = new Calendar(DateTime.Now.AddMonths(1).Month, DateTime.Now.AddMonths(1).Year).Css("calendar_big").ShowMonth(true).ShowYear(true);

			foreach (var evt in events.Where(e => e.EventDate.Month == DateTime.Now.Month))
				calendar1.AddLink(evt.EventDate.Day, "#event-" + evt.id_Event, Color.Empty);

			foreach (var evt in events.Where(e => e.EventDate.Month == DateTime.Now.AddMonths(1).Month))
				calendar2.AddLink(evt.EventDate.Day, "#event-" + evt.id_Event, Color.Empty);
		}
	}
}
